<template>
  <van-popup
    v-model:show="show"
    position="bottom"
    round
    :style="{ height: '70%' }"
  >
    <div class="title">兑换记录</div>
    <div class="recordsWrap">
      <van-pull-refresh
        v-model="refreshing"
        :class="[{'emptyPullRefresh': (records.length===0)}]"
        @refresh="onRefresh"
      >
        <van-list
          v-if="records.length > 0"
          v-model:loading="loading"
          :finished="finished"
          finished-text="已加载全部数据"
          @load="loadList"
        >
          <div v-for="(item, index) in records" :key="index" class="recordItem">
            <div class="exchangeIcon">
              <img :src="item.Icon">
            </div>
            <div class="exchangeContent">
              <div class="exchangeContentTop">
                <div class="exchangeName">{{ getExchangeName(item) || '暂无' }}</div>
                <div class="exchangeSources">
                  <div class="sourceList">
                    <div v-for="(sourceItem, sourceItemIndex) in item.Sources" :key="sourceItemIndex" class="sourceItem">
                      <div class="sourceIcon">
                        <img :src="sourceItem.Icon">
                      </div>
                      <div class="sourceValue">
                        {{ sourceItem.Count }}<span v-if="sourceItemIndex < item.Sources.length - 1">+</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="exchangeContentBottom">{{ formatDate(item.CreateTime) }}</div>
            </div>
          </div>
        </van-list>
        <div v-else class="emptyRecordBlock">
          暂无记录
        </div>
      </van-pull-refresh>
    </div>
  </van-popup>
</template>
<script>
import { formatDate } from '@/lib/js/utils/transform'
import { getExchangeName } from '@/lib/js/utils/activity'
import $api from '../api'
export default{
  props: {
    isShow:{
      type: Boolean,
      default:false
    },

    defaultRecords: {
      type: Array,
      default(){
        return []
      }
    }

  },
  emits:['update:isShow'],
  data(){
    return {
      show: false,
      finished: false,
      loading: false,
      refreshing: false,
      page: 2,
      records: {
        type: Array,
        default(){
          return []
        }
      }
    }
  },

  watch: {
    isShow:{
      handler(value){
        this.show = value
      },
      immedate:true
    },

    show:{
      handler(value){
        this.$emit('update:isShow', value)
      },
      immedate:true
    },

    defaultRecords:{
      handler(value){
        this.records = value
      },
      immedate:true
    }
  },
  methods: {
    formatDate(date){
      return formatDate(date, 'YYYY-MM-DD HH:mm:ss')
    },

    async loadList(isRefresh){
      this.loading = true
      try{
        const res =  await $api.loadRecord({ page: this.page, limit: 20 })
        const { data, total } = res
        this.page++
        if(isRefresh){
          this.records = data
          this.refreshing = false
        }else{
          this.records = this.records.concat(data)
        }
        if(this.records.length >= total){
          this.finished = true
        }
      }finally{
        this.loading = false
      }
    },

    onRefresh(){
      this.page = 1
      this.finished = false
      this.loadList(true)
    },

    getExchangeName(item){
      const nextItem = {
        type: item.TargetType,
        count: item.TargetCount,
        name: item.Name
      }
      return getExchangeName(nextItem)
    }
  }
}
</script>
<style lang="scss" scoped>
.title{
  color: #FF7580;
  font-size: 0.32rem;
  padding: 0.46rem 0 0.27rem 0.3rem;
  position: relative;
}

.recordsWrap{
  position: absolute;
  top:1.15rem;
  bottom:0;
  left:0;
  right:0;
  overflow-y: auto;
  color: #000;
  .emptyPullRefresh{
    position: absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
  }
  .emptyRecordBlock{
    position: absolute;
    top:0;
    bottom: 1.7rem;
    left:0;
    right:0;
    color: #acacac;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.title:before{
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  border-bottom: 1px solid rgba(172,172,172, 0.2);
  transform: scaleY(0.8);
  transform-origin: left top;
}
.recordItem{
  display: flex;
  justify-content: flex-start;
  padding: 0.3rem;
  border-bottom: 1px rgba(172,172,172, 0.2) solid;
  .exchangeIcon{
    width: 0.96rem;
    height: 0.96rem;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 0.18rem;
    >img{
      max-height: 100%;
      max-width: 100%;
    }
  }
  .exchangeContent{
    flex: 1;
    .exchangeContentTop{
      font-size: 0.32rem;
      padding-top: 0.11rem;
      display: flex;
      justify-content:flex-start;
      .exchangeName{
        flex: 1
      }
      .sourceList{
        display: flex;
        justify-content: flex-end;
        .sourceItem{
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 0.2rem;
          .sourceIcon{
            width: 0.22rem;
            height: 0.22rem;
            display: flex;
            justify-content: center;
            align-items: center;
            >img{
              max-width: 100%;
              max-height: 100%;
            }
          }
          .sourceValue{
            opacity: 0.6;
          }
        }
      }
    }
    .exchangeContentBottom{
      font-size: 0.24rem;
      color:#ACACAC;
    }
  }
}



</style>
